package cn.service.impl;

import cn.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.Brand;
import entity.Result;
import mapper.BrandMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    BrandMapper brandMapper;

    @Override
    public Result queryAllBrand(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Brand> page = (Page<Brand>) brandMapper.queryAllBrand();
        return Result.success((int)page.getTotal(),page.getResult());
    }

    @Override
    public void updateIs_show(Integer brand_id,Integer is_show) {
        brandMapper.updateIs_show(brand_id,is_show);
    }

    @Override
    public void updateIs_manufacturer(Integer brand_id, Integer is_manufacturer) {
        brandMapper.updateIs_manufacturer(brand_id,is_manufacturer);
    }

    @Override
    public Result deleteBrandByBrand_id(List<Integer> brandLists) {
        if (brandMapper.deleteBrandByBrand_id(brandLists) > 0) {
            //删除成功
            return Result.success(200,"批量删除成功!");
        } else {
            //删除失败
            return Result.fail(500,"删除失败,请联系管理员删除!");
        }
    }

    @Override
    public Result queryBrandByBrand_name(String brand_name) {//根据品牌名查询品牌
        if (brandMapper.queryBrandByBrand_name(brand_name) != null) {
            return Result.success(brandMapper.queryBrandByBrand_name(brand_name));
        } else {
            return null;
        }
    }

    @Override
    public Result addBrand(Brand brand) {//添加品牌
        if (Objects.isNull(queryBrandByBrand_nameReturnObj(brand.getBrand_name()))) {
            //允许添加
            if (brandMapper.insert(brand) > 0) {
                //添加成功
                return Result.success(200,"添加成功!");
            } else {
                //添加失败
                return Result.fail(500,"服务繁忙,请稍后重试!");
            }
        } else {
            return Result.fail(500,"添加失败,品牌名称已存在!");
        }
    }

    @Override
    public Brand queryBrandByBrand_nameReturnObj(String brand_name) {
        return brandMapper.queryBrandByBrand_nameReturnObj(brand_name);
    }

    @Override
    public Result deleteByPrimaryKey(Integer brand_id) {
        if (brandMapper.deleteByPrimaryKey(brand_id) > 0) {
            return Result.success(200,"删除成功!");
        } else {
            return Result.fail(500,"删除失败!");
        }
    }

    @Override
    public Result selectByPrimaryKey(Integer brand_id) {
        return Result.success(brandMapper.selectByPrimaryKey(brand_id));
    }

    @Override
    public Result updateBrand(Brand brand) {
        if (brandMapper.updateByPrimaryKeyWithBLOBs(brand) > 0) {
            return Result.success(200,"修改成功!");
        } else {
            return Result.fail(500,"修改失败!");
        }
    }


}
